#pragma once
#include<iostream>
#include<vector>
using namespace std;
namespace zjs 
{
	template<class T,class Container = vector<T>,class Compare = Less<T>>
	class priority_queue 
	{
	public:
		priority_queue(){}

		template<class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
			:_con(first,last)
		{
			for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
				AdjustDown(i);
		}

		void AdjustUp(int child) {
			Compare com;
			for (int parent = (child - 1) / 2; child > 0; child = parent, parent = (child - 1) / 2) {
				if (com(_con[parent], _con[child]))
					swap(_con[parent], _con[child]);
				else
					break;
			}
		}


		const T& top() const
		{
			return _con[0];
		}

		T& top()
		{
			return _con[0];
		}

		void AdjustDown(int parent) {
			Compare com;
			int child = parent * 2 + 1;
			while (child<_con.size()) {
				if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
					child++;
				if (com(_con[parent], _con[child])) {
					swap(_con[child], _con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else {
					break;
				}
			}
		}

		void push(const T&x)
		{
			_con.push_back(x);
			AdjustUp(_con.size()-1);
		}

		void pop()
		{
			swap(_con.back(), _con[0]);
			_con.pop_back();
			AdjustDown(0);
		}

		size_t size() const
		{
			return _con.size();
		}

		bool empty() const
		{
			return _con.empty();
		}
	private:
		Container _con;
	};
}